package net.towee.server.ftp.files;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import net.towee.model.repo.AttachmentIdentity;
import net.towee.model.repo.DocumentData;
import net.towee.model.repo.DocumentNode;
import net.towee.server.persistence.AttachmentStore;
import net.towee.server.persistence.DocumentStore;
import net.towee.shared.exceptions.PersistenceException;

import org.apache.ftpserver.ftplet.FtpFile;

public class AttachmentsListFtpFile extends AbstractFtpFile {

	private final AttachmentStore attStore;
	private final DocumentStore docStore;
	private final DocumentNode node;

	public AttachmentsListFtpFile(DocumentNode node, DocumentStore docStore, AttachmentStore attStore) {
		super("attachments", true, 0, "", "", FtpFileType.ATTACHMENT, 
				"/" + node.getDocumentIdentity().getGroup().getAlias() + node.getPath() + "/"
				+ node.getDocumentIdentity().getName() + "/" + "attachments");
		
		this.docStore = docStore;
		this.attStore = attStore;
		this.node = node;
	}

	@Override
	public long getLastModified() {
		return 0;
	}

	@Override
	public List<FtpFile> listFiles() {
		try {
			DocumentData doc = docStore.getDocument(node.getDocumentIdentity().getId(), "attach");
			List<FtpFile> files = new ArrayList<FtpFile>();
			
			for(AttachmentIdentity id : doc.getAttachments().getAttachmentList()){
				files.add( new AttachmentFtpFile(node, id, attStore) );
			}
			
			return files;
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Override
	public OutputStream createOutputStream(long offset) throws IOException {
		return null;
	}

	@Override
	public InputStream createInputStream(long offset) throws IOException {
		return null;
	}

}
